/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hitechbangla.fctu.form;

/**
 *
 * @author Hi-techban gla_2
 */

import com.hitechbangla.fctu.base.GameScene;
import java.awt.Color;
import java.awt.Window.Type;
import java.awt.event.ActionEvent;
import java.net.InetAddress;
import javax.swing.JOptionPane;
import org.multichat.client.ChatClient;


public class Voicechat extends javax.swing.JInternalFrame {

    /**
     * Creates new form Voicechat
     */
    
     
    static ChatClient chatClient;
    //GameScene gameScene;
    public Voicechat(int i)
    {
     chatClient = new ChatClient();
    }
    
    public Voicechat() {
        
        super("", true, true, true, true);
        
        //gameScene = new GameScene();
        initComponents();
        //JOptionPane.showMessageDialog(null, "inside voice chat newLayout");
    }
    
    
    public void startTalking()
    {
        if( chatClient.recording )
                     {
                            chatClient.stopRecording();
                            //btnTalk.setText("Talk");
                            //setButtonTalkColor( btnExit.getBackground() );
                            //System.out.println("stop talking from f5 pressed inside voicechat ");
                     }
                     else
                     {
                            if( chatClient.canrecord && chatClient.IAmMute != true )
                            {
                                chatClient.startRecording();
                                //System.out.println("start talking from f5 pressed inside voicechat ");
                                //btnTalk.setText("Stop Talk");
                                //setButtonTalkColor( Color.red );
                            }else
                            {
                                chatClient.recording = !chatClient.recording;
                            }
                       }
                      chatClient.recording = !chatClient.recording;
                      
                      //System.out.println("start talking from f5 pressed inside voicechat ");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Voice Control");
        setType(java.awt.Window.Type.UTILITY);

        jButton1.setText("Talk");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton1MouseReleased(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton1MousePressed(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Connect");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jButton2)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(37, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        
        chatClient.chkHandsFreeActionPerformed(evt);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        String computername = null;
        //String userName = gameScene.userName;
        try{
            computername=InetAddress.getLocalHost().getHostName();
            //System.out.println(computername);
            }
        catch (Exception e){
            //System.out.println("Exception caught ="+e.getMessage());
            }
        
        chatClient.btnConnectActionPerformed(evt);
        
        chatClient.txtInput.setText(computername);
        //jTextField1.setEditable(false);
        chatClient.txtInputActionPerformed(new ActionEvent(evt, 0, null));
        jButton2.setText("Connected");
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MousePressed
        // TODO add your handling code here:
        Runnable task = new Runnable() {

            @Override
            public void run() {
                //throw new UnsupportedOperationException("Not supported yet.");
                if (chatClient.canrecord && chatClient.IAmMute != true) {
                    chatClient.startRecording();
                    chatClient.btnTalk.setText("Stop Talk");
                    chatClient.setButtonTalkColor(Color.red);
                } else {
                    chatClient.recording = !chatClient.recording;
                }
                toggleRecording();
                //System.out.println("recoridng started");
            }
        };
        Thread t = new Thread(task);
        t.start();
    }//GEN-LAST:event_jButton1MousePressed

    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseReleased
        // TODO add your handling code here:

        
        if (chatClient.recording) {
            chatClient.stopRecording();
            chatClient.btnTalk.setText("Talk");
            chatClient.setButtonTalkColor(chatClient.btnExit.getBackground());
        }
        //recording = !recording;
        
        toggleRecording();
        //System.out.println("recoridng stopped");

    }//GEN-LAST:event_jButton1MouseReleased

    private void toggleRecording() {
        chatClient.recording = !chatClient.recording;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Voicechat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Voicechat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Voicechat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Voicechat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Voicechat().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration//GEN-END:variables

    private void setType(Type type) {
        //throw new UnsupportedOperationException("Not yet implemented");
    }
}
